layui.use(['form', 'layer', 'table'], function () {
	var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : top.layer,
		$ = layui.jquery,
		table = layui.table,
		company_id = sessionStorage["company_id"],
		scale = sessionStorage["scale"],
		user_id = sessionStorage["userId"];
	if (user_id == 3) {
		loadCompanyList(form, $);
		$('.company').removeClass("layui-hide");
	}
	//用户列表
	var tableIns = table.render({
		elem: '#examList',
		url: baseUrl + 'exam/getExams.php',
		where: {
			scale,
			company_id
		},
		skin: 'row',
		even: true, //开启隔行背景
		toolbar: true,
		defaultToolbar: ['filter'],
		cellMinWidth: 95,
		page: true,
		// height : "full-120",
		limits: [10, 15, 20, 25],
		limit: 10,
		id: "examListTable",
		cols: [
			[
				{
					type: 'checkbox',
					field: 'left',
					align: 'center'
				},
				{
					title: '序号',
					type: "numbers"
				}, {
					field: 'id',
					title: 'ID',
					hide: true
				}, {
					field: 'account',
					title: '用户名'
				},
				{
					field: 'password',
					title: '密码',
					align: 'center',
				},
				{
					field: 'loginCount',
					title: '登录次数',
					align: 'center',
					style: 'cursor: pointer;',
					event: 'loginCount'
				},
				{
					title: '操作',
					width: 150,
					templet: '#examListBar',
					fixed: "right",
					align: "center"
				}
			]
		],
		done: function (res, curr, count) {// done 数据表格加载完成
			if (scale == 1 || scale == 2) {
				$('.Pldelect').css('display', 'inline-block')
			}
			else if (scale == 3) {
				$('#manager').css('display', 'block')
			}
			else {
				$('#manager').css('display', 'none')
			}
		}
	});

	$('.searchVal').on('keyup', function (event) {
		if (event.keyCode != 13) return
		$(".search_btn").click()
	})
	//搜索
	$(".search_btn").on("click", function () {
		table.reload("examListTable", {
			page: {
				curr: 1 //重新从第 1 页开始
			},
			where: {
				searchText: $(".searchVal").val(), //搜索的关键字
				company_id: user_id == 3 ? $("#company").val() : company_id
			}
		})
	});
	//添加用户
	function editExam(edit) {
		var index = layui.layer.open({
			title: "账号编辑",
			type: 2,
			area: ['80%', '80%'],
			fixed: false, //不固定
			maxmin: true,
			shadeClose: true,
			content: "examEdit.html",
			success: function (layero, index) {
				var body = layui.layer.getChildFrame('body', index);
				if (edit) {//修改
					body.find(".addOrUpdate").val(1);
					body.find(".examId").val(edit.id);
					body.find(".account").val(edit.account);
					body.find(".password").val(edit.password);
				} else {//添加
					body.find(".addOrUpdate").val(0);
				}
				setTimeout(function () {
					layui.layer.tips('点击此处返回用户列表', '.layui-layer-setwin .layui-layer-close', {
						tips: 3
					});
				}, 500)
			}
		})
		//layui.layer.full(index);
		sessionStorage.setItem("index", index);
		//改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
	}
	$(".addNews_btn").click(function () {
		editExam();
	})
	//批量删除
	$(".deleteBatch").click(function () {
		if (scale == 1) {
			var data = table.checkStatus('examListTable').data;
			if (data.length <= 0) return;
			var ids = [];
			data.forEach(item => {
				ids.push(item.id);
			})
			//请求批量删除
			$.ajax({
				url: baseUrl + "exam/deleteExam.php",
				data: {
					ids: ids
				},
				type: "POST",
				dataType: "text",
				success: function (data) {
					if (data == "1") {
						tableIns.reload();
						layer.msg('批量删除账号成功！')
					} else {
						layer.msg('批量删除账号失败！')
					}
				}
			})
		} else {
			layer.msg('超级管理员才可以批量删除！')
		}
	})
	//列表操作
	table.on('tool(examList)', function (obj) {
		var layEvent = obj.event,
			data = obj.data;
		if (layEvent === 'edit') { //编辑
			editExam(data);
		} else if (layEvent === 'loginCount') {
			console.log('loginCount' + data.id);

			layer.prompt({
				formType: 2,
				title: '修改账号 [' + data.account + '] 的 登录次数',
				value: data.loginCount
			}, function (value, index) {
				layer.close(index);
				$.ajax({
					url: baseUrl + "exam/updateExam.php",
					data: {
						id: data.id,
						account: data.account,
						password: data.password,
						loginCount: value
					},
					type: "POST",
					dataType: "text",
					success: function (data) {
						if (data == "1") {
							//同步更新表格和缓存对应的值
							obj.update({
								loginCount: value
							});
						}
					}
				})
			});
		} else if (layEvent === 'del') { //删除
			layer.confirm('确定删除账号 ' + data.account + ' ？', {
				icon: 3,
				title: '删除确认'
			}, function (index) {
				$.ajax({
					url: baseUrl + "exam/deleteExam.php",
					data: {
						id: data.id
					},
					type: "POST",
					dataType: "text",
					success: function (data) {
						if (data == "1") {
							tableIns.reload();
							layer.close(index);
							layer.msg('删除账号成功！')
						} else {
							layer.msg('删除账号失败！')
						}
					}
				})
			});
		}
	});

	table.on('rowDouble(examList)', function (obj) {
		editExam(obj.data);
	});
})
